package com.weichuang.test;

import com.weichuang.domain.Order;
import com.weichuang.mapper.OrderMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisTest2 {
    private OrderMapper orderMapper;

    @Before
    public void before() throws IOException {
        //获取核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获取session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获取session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取Mybatis框架生成的Mapper接口的实现类
        orderMapper = sqlSession.getMapper(OrderMapper.class);
    }

    @Test
    public void testFindByUid(){
        List<Order> orderList = orderMapper.findByUid(3);
        for (Order order : orderList){
            System.out.println(order);
        }
    }

    @Test
    public void testFindAll(){
        List<Order> orderList = orderMapper.findAll();
        for (Order order : orderList){
            System.out.println(order);
        }
    }
}
